package com.company.cloud.crm.modules.mgr.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.crm.modules.base.entity.CustomerAllocationLog;
import com.company.cloud.crm.modules.base.entity.CustomerInfo;
import com.company.cloud.crm.modules.base.service.CustomerAllocationLogService;
import com.company.cloud.crm.modules.base.service.CustomerInfoService;
import com.company.cloud.crm.modules.cache.models.CustomerLogModel;
import com.company.cloud.crm.modules.cache.models.CustomerModel;
import com.company.cloud.crm.modules.cache.utils.CrmBizChangeUtils;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerAllocationParam;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerBackParam;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerQuery4PageParam;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerReceiveParam;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerSaveParam;
import com.company.cloud.crm.modules.mgr.controller.req.CustomerUpdateParam;
import com.company.cloud.crm.modules.mgr.service.CustomerMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Authc
@RestController
@RequestMapping("/crm/mgr/cust")
@Api(tags="CRM_Mgr_客户管理")
public class CustomerMgrController {

	@Autowired
	private CustomerMgrService customerMgrService;

	@Autowired
	private CustomerInfoService customerInfoService;

	@Autowired
	private CustomerAllocationLogService customerAllocationLogService;
	
	@PostMapping("/query")
	@ApiOperation(value="查询", response=CustomerModel.class)
	public BaseResult query(@RequestBody CustomerQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<CustomerInfo> page = customerInfoService.page(
				new Page<>(params.getPage(), params.getRows()), 
				 new QueryWrapper<CustomerInfo>()
				 .eq("company_no", userInfo.getCompanyNo())
				);
		IPage<CustomerModel> data=page.convert(new Function<CustomerInfo, CustomerModel>() {
			@Override
			public CustomerModel apply(CustomerInfo entity) {
				return CrmBizChangeUtils.getCustomer(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/log")
	@ApiOperation(value="查询分配日志", response=CustomerLogModel.class)
	public BaseResult log(@RequestParam("custNo") String custNo) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<CustomerAllocationLog> logList=customerAllocationLogService.list(
				new QueryWrapper<CustomerAllocationLog>()
				.eq("company_no", userInfo.getCompanyNo())
				.eq("cust_no", custNo)
				.orderByDesc("start_date")
				);
		if(CollectionUtils.isEmpty(logList)) {
			return BaseResult.ok();
		}
		List<CustomerLogModel> data=new ArrayList<CustomerLogModel>();
		logList.forEach(item->{
			data.add(CrmBizChangeUtils.getCustomerLog(item));
		});
		
		return BaseResult.ok(data);
	}
	
	
	
	@PostMapping("/allocationByPool")
	@ApiOperation(value="从公海分配客户", response=BaseResult.class)
	@StaffLogc(title = "从公海分配客户", action = "从公海分配客户", actionAdo = "客户管理")
	public BaseResult allocationByPool(@RequestBody CustomerAllocationParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.allocationByPool(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/receiveByPool")
	@ApiOperation(value="从公海领取客户", response=BaseResult.class)
	@StaffLogc(title = "从公海领取客户", action = "从公海领取客户", actionAdo = "客户管理")
	public BaseResult receiveByPool(@RequestBody CustomerReceiveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		CustomerAllocationParam allocationParam=new CustomerAllocationParam();
		allocationParam.setCustNos(params.getCustNos());
		allocationParam.setSaleUserId(userInfo.getId());
		customerMgrService.allocationByPool(allocationParam, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/backByPool")
	@ApiOperation(value="退回到公海", response=BaseResult.class)
	@StaffLogc(title = "退回到公海", action = "退回到公海", actionAdo = "客户管理")
	public BaseResult backByPool(@RequestBody CustomerBackParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.backByPool(params, userInfo);
		return BaseResult.ok();
	}
	
	
	@PostMapping("/allocation")
	@ApiOperation(value="分配客户", response=BaseResult.class)
	@StaffLogc(title = "分配客户", action = "分配客户", actionAdo = "客户管理")
	public BaseResult allocation(@RequestBody CustomerAllocationParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.allocation(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/addByMgr")
	@ApiOperation(value="管理员新增客户", response=BaseResult.class)
	@StaffLogc(title = "管理员新增客户", action = "管理员新增客户", actionAdo = "客户管理")
	public BaseResult addByMgr(@RequestBody CustomerSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.save(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/addByEmployee")
	@ApiOperation(value="员工新增客户", response=BaseResult.class)
	@StaffLogc(title = "员工新增客户", action = "员工新增客户", actionAdo = "客户管理")
	public BaseResult addByEmployee(@RequestBody CustomerSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		params.setSaleUserId(userInfo.getId());
		customerMgrService.save(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/edit")
	@ApiOperation(value="修改", response=BaseResult.class)
	@StaffLogc(title = "员工修改客户", action = "员工修改客户", actionAdo = "客户管理")
	public BaseResult edit(@RequestBody CustomerUpdateParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.update(params, userInfo);
		return BaseResult.ok();
	}
	
	@GetMapping("/get")
	@ApiOperation(value="获取", response=CustomerModel.class)
	public BaseResult get(@RequestParam("custNo") String custNo) {
		CustomerModel data= customerMgrService.get(custNo);
		return BaseResult.ok(data);
	}

	@GetMapping("/getByUserId")
	@ApiOperation(value="通过用户ID获取", response=CustomerModel.class)
	public BaseResult getByUserId(@RequestParam("userId") Integer userId) {
		CustomerModel data= customerMgrService.getByUserId(userId);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/del")
	@ApiOperation(value="删除", response=BaseResult.class)
	@StaffLogc(title = "员工删除客户", action = "员工删除客户", actionAdo = "客户管理")
	public BaseResult del(@RequestParam("custNo") String custNo) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		customerMgrService.del(custNo, userInfo);
		return BaseResult.ok();
	}
	
}
